require('./tabs.m.htm')

export default {
    setup() {
        // require('./x-demo/demo.json')
        let router = MFCRouter.useRouter();
        // debugger;
        let route = MFCRouter.useRoute()
        window.$v3.$router=router;
        window.$v3.$router.beforeEach((to,from,next)=>{
            // console.log('to and from --:',to,from)
            next()
        })
        window.$v3.$route=route;
        window.$v3.$bus.$on('B_ROUTE_COMP_RENDERED', (config) => {
            // debugger;
            // path=path.split('/').reverse()[0]
            handleOpen(config?.meta?.title)
            defaultActiveMenuItem.value=config?.meta?.title
        })

        let defaultActiveMenuItem=ref('首页')

        const handleOpen = (a, b) => {
            // alert(a)
            // debugger
            let v = editableTabs.value;
            // debugger;
            if (!v.find(s => s.title == a)) {
                v.push({
                    title: a,
                    name: a
                })
            }
            editableTabsValue.value = a;
        }


        const editableTabs = ref([

        ])


        const editableTabsValue = ref('')

        watch(editableTabsValue, (nv) => {
            let obj = window.MFC_ROUTE_CONFIG.find(s => s?.meta?.title == nv)
            // debugger
            if (obj) {
                router.push(obj.path)
            }
        })


        return {
            async readJSON(){
                // window.r=require;
                let res=await require('./x-demo/demo.json')
                console.log(res)
            },
            async readESM(){
                let res=await require('./x-demo/esm-demo.js')
                console.log(res)
            },
            defaultActiveMenuItem,
            handleOpen,
            router,
            editableTabsValue,
            editableTabs,
            locationHref: location.href,
            removeTab(a) {
                if (editableTabs.value.length == 1) {
                    ElementPlus.ElMessage.warning('至少保留一项tab')
                    return false;
                }

                let idx = editableTabs.value.findIndex(s => s.name == a)
                editableTabs.value.splice(idx, 1)

                if (a == editableTabsValue.value) {
                    editableTabsValue.value = editableTabs.value[idx - 1].name
                }

            },

            route
        }
    }
}